<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2006 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

class CMS_Comment extends Pluf_Model
{
    function init()
    {
        $this->_a['verbose'] = __('comment');
        $this->_a['table'] = 'cms_comments';
        $this->_a['model'] = 'CMS_Comment';
        $this->_a['cols'] = array(
                            'id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Sequence',
                                  'blank' => true, 
                                  ),
                            'model' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'verbose' => __('model'),
                                   ),
                            'model_id' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Integer',
                                  'blank' => false,
                                  'verbose' => __('model id'),
                                   ),
                            'website' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Foreignkey',
                                  'model' => 'CMS_Website',
                                  'blank' => false,
                                  'verbose' => __('website'),
                                   ),
                            'content' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Text',
                                  'blank' => false,
                                  'verbose' => __('content'),
                                  ),
                            'content_format' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 10,
                                  'default' => 'text/plain',
                                  'verbose' => __('format of the content'),
                                  ),
                            'author' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 50,
                                  'verbose' => __('author'),
                                  ),
                            'author_email' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Email',
                                  'blank' => false,
                                  'verbose' => __('email'),
                                  ),
                            'author_website' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => true,
                                  'size' => 150,
                                  'verbose' => __('website'),
                                  ),
                            'status' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Integer',
                                  'default' => 1,
                                  'blank' => false,
                                  'verbose' => __('status'),
                                  'choices' => array(
                                                     __('On line') => 1,
                                                     __('Off line') => 2,
                                                     __('Junk') => 6,
                                                     ),
                                  ),
                            'why_junk' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar', 
                                  'size' => 250,
                                  'blank' => true,
                                  'verbose' => __('why is spam'),
                                  ),
                            'creation_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('creation date'),
                                  ),
                            'modif_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('modification date'),
                                  ),
                            );
        $this->_a['idx'] = array(
                                 'website_idx' =>
                                 array(
                                       'col' => 'website',
                                       'type' => 'normal',
                                       ),
                                 'model_idx' =>
                                 array(
                                       'col' => 'model, model_id',
                                       'type' => 'normal',
                                       ),
                                 );
        $this->_a['views'] = array();
    }

    /**
     * Set the date times.
     *
     */
    function preSave()
    {
        if (!($this->id > 0)) {
            $this->creation_dtime = gmdate('Y-m-d H:i:s');
        }
        $this->modif_dtime = gmdate('Y-m-d H:i:s');
    }

    /**
     * Retrieve the comments for a given model and id.
     *
     * @param mixed The model, usually just $this
     * @param array The parameters you would put in a getList call (array())
     * @return ArrayObject List of matching comments.
     */
    public static function retrieve($obj, $params=array())
    {
        $sql = new Pluf_SQL('model=%s AND model_id=%s',
                            array($obj->_a['model'], $obj->id));
        if (isset($params['filter'])) {
            $params['filter'] = '('.$params['filter'].') AND ('.$sql->gen().')';
        } else {
            $params['filter'] = $sql->gen();
        }
        return Pluf::factory('CMS_Comment')->getList($params);
    }
}
